home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / wrlib / testgrad.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-03-28  |  6.1 KB  |  213 lines

  1.  
  2.  
  3. #include <X11/Xlib.h>
  4. #include "wraster.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #ifdef BENCH
  9. #include <sys/time.h>
  10. #include <time.h>
  11. #endif
  12.  
  13.  
  14. Display *dpy;
  15. Window win;
  16. RContext *ctx;
  17. RImage *imgh, *imgv, *imgd;
  18. Pixmap pix;
  19. char *ProgName;
  20.  
  21.  
  22. void
  23. print_help()
  24. {
  25.     printf("usage: %s [-options] color1 [color2 ...]\n", ProgName);
  26.     puts("options:");
  27.     puts(" -m         match  colors");
  28.     puts(" -d        dither colors (default)");
  29.     puts(" -c <cpc>    colors per channel to use");
  30.     puts(" -v <vis-id>    visual id to use");
  31. }
  32.  
  33. #ifdef BENCH
  34. #include "bench.h"
  35. #endif
  36. int main(int argc, char **argv)
  37. {
  38.     RContextAttributes attr;
  39.     RColor **colors = NULL;
  40.     int i, rmode = RDitheredRendering, ncolors = 0, cpc = 4;
  41.     char **color_name;
  42.     XColor color;
  43.     XSetWindowAttributes val;
  44.     int visualID = -1;
  45. #ifdef BENCH
  46.     double t1, t2, total, t, rt;
  47.     struct timeval timev;
  48. #endif
  49.  
  50.     ProgName = strrchr(argv[0],'/');
  51.     if (!ProgName)
  52.       ProgName = argv[0];
  53.     else
  54.       ProgName++;
  55.  
  56.     color_name = (char **) malloc(sizeof(char*) * argc);
  57.     if(color_name == NULL) {
  58.         fprintf(stderr, "Cannot allocate memory!\n");
  59.         exit(1);
  60.     }
  61.  
  62.     if (argc>1) {
  63.     for (i=1; i<argc; i++) {
  64.             if (strcmp(argv[i], "-m")==0) {
  65.                 rmode = RBestMatchRendering;
  66.             } else if (strcmp(argv[i], "-d")==0) {
  67.                 rmode = RDitheredRendering;
  68.             } else if (strcmp(argv[i], "-c")==0) {
  69.                 i++;
  70.                 if (i>=argc) {
  71.             fprintf(stderr, "too few arguments for %s\n", argv[i-1]);
  72.                     exit(0);
  73.                 }
  74.                 if (sscanf(argv[i], "%i", &cpc)!=1) {
  75.                     fprintf(stderr, "bad value for colors per channel: \"%s\"\n", argv[i]);
  76.                     exit(0);
  77.                 }
  78.             } else if (strcmp(argv[i], "-v")==0) {
  79.                 i++;
  80.                 if (i>=argc) {
  81.             fprintf(stderr, "too few arguments for %s\n", argv[i-1]);
  82.                     exit(0);
  83.                 }
  84.                 if (sscanf(argv[i], "%i", &visualID)!=1) {
  85.                     fprintf(stderr, "bad value for visual ID: \"%s\"\n", argv[i]);
  86.                     exit(0);
  87.                 }
  88.             } else if (argv[i][0] != '-') {
  89.                 color_name[ncolors++] = argv[i];
  90.             } else {
  91.                 print_help();
  92.                 exit(1);
  93.             }
  94.     }
  95.     }
  96.  
  97.     if (ncolors == 0) {
  98.         print_help();
  99.         exit(1);
  100.     }
  101.  
  102.     dpy = XOpenDisplay("");
  103.     if (!dpy) {
  104.     puts("cant open display");
  105.     exit(1);
  106.     }
  107.     attr.flags = RC_RenderMode | RC_ColorsPerChannel;
  108.  
  109.     attr.render_mode = rmode;
  110.     attr.colors_per_channel = cpc;
  111.  
  112.     if (visualID >= 0) {
  113.         attr.flags |= RC_VisualID;
  114.         attr.visualid = visualID;
  115.     }
  116.  
  117.     ctx = RCreateContext(dpy, DefaultScreen(dpy), &attr);
  118.  
  119.     if (!ctx) {
  120.     printf("could not initialize graphics library context: %s\n",
  121.            RMessageForError(RErrorCode));
  122.     exit(1);
  123.     }
  124.  
  125.     colors = malloc(sizeof(RColor*)*(ncolors+1));
  126.     for (i=0; i<ncolors; i++) {
  127.         if (!XParseColor(dpy, ctx->cmap, color_name[i], &color)) {
  128.             printf("could not parse color \"%s\"\n", color_name[i]);
  129.             exit(1);
  130.         }
  131.         else {
  132.             colors[i] = malloc(sizeof(RColor));
  133.             colors[i]->red = color.red >> 8;
  134.             colors[i]->green = color.green >> 8;
  135.             colors[i]->blue = color.blue >> 8;
  136.             printf("0x%02x%02x%02x\n", colors[i]->red, colors[i]->green,
  137.                    colors[i]->blue);
  138.         }
  139.     }
  140.     colors[i] = NULL;
  141.  
  142.     val.background_pixel = ctx->black;
  143.     val.colormap = ctx->cmap;
  144.     val.backing_store = Always;
  145. #ifdef BENCH
  146.     win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 250, 250,
  147.                         0, ctx->depth, InputOutput, ctx->visual,
  148.                         CWColormap|CWBackPixel|CWBackingStore, &val);
  149. #else
  150.     win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 750, 250,
  151.                         0, ctx->depth, InputOutput, ctx->visual,
  152.                         CWColormap|CWBackPixel|CWBackingStore, &val);
  153. #endif
  154.     XMapRaised(dpy, win);
  155.     XFlush(dpy);
  156.  
  157. #ifdef BENCH
  158.     rt = 0;
  159.     gettimeofday(&timev, NULL);
  160.     t = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
  161.     for (i=0; i<9; i++) {
  162.     if (i>0)
  163.         printf("\nrepeating...\n\n");
  164.     gettimeofday(&timev, NULL);
  165.     t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
  166.     if (i%3==0)
  167.         imgh = RRenderMultiGradient(550, 550, colors, RGRD_HORIZONTAL);
  168.     else if (i%3==1)        
  169.         imgh = RRenderMultiGradient(550, 550, colors, RGRD_VERTICAL);
  170.     else
  171.         imgh = RRenderMultiGradient(550, 550, colors, RGRD_DIAGONAL);
  172.  
  173.     gettimeofday(&timev, NULL);
  174.     t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
  175.     total = t2 - t1;
  176.     printf("gradient rendered in %f sec\n", total);
  177.     
  178.     RConvertImage(ctx, imgh, &pix);
  179.     gettimeofday(&timev, NULL);
  180.     t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
  181.     total = t1 - t2;
  182.     rt += total;
  183.     printf("image converted in %f sec\n", total);
  184.  
  185.     XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 0, 0);
  186.  
  187.     XFlush(dpy);
  188.     }
  189.     t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
  190.     printf("------------------------------------------\n");
  191.     printf("%i images processed in %f sec\n", i, t1-t);
  192.     printf("average time per convertion %f sec\n", rt/i);
  193.     printf("------------------------------------------\n");
  194. #else
  195.     imgh = RRenderMultiGradient(250, 250, colors, RGRD_HORIZONTAL);
  196.     imgv = RRenderMultiGradient(250, 250, colors, RGRD_VERTICAL);
  197.     imgd = RRenderMultiGradient(250, 250, colors, RGRD_DIAGONAL);
  198.     RConvertImage(ctx, imgh, &pix);
  199.     XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 0, 0);
  200.  
  201.     RConvertImage(ctx, imgv, &pix);
  202.     XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 250, 0);
  203.  
  204.     RConvertImage(ctx, imgd, &pix);
  205.     XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 500, 0);
  206.  
  207.     XFlush(dpy);
  208. #endif
  209.  
  210.     getchar();
  211.     return 0;
  212. }
  213.